package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsContentMapper newsContentMapper;
    public PageInfo<News> listAll(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo<>(newsMapper.selectForList(qo));
    }

    public News getById(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    public Integer queryCollectCount(Long newsId) {
        Integer collectCount = newsMapper.selectCollectCount(newsId);
        return collectCount;
    }

    public void updateCollectCount(int collectCount, Long newsId) {
        newsMapper.updateCollectCount(collectCount,newsId);

    }

    public NewsContent getContentByNewsId(Long id) {
        return newsContentMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(News entity) {
        //获取日报内容对象
        NewsContent newsContent = entity.getNewsContent();
        if (entity.getId()!=null){
            newsMapper.updateByPrimaryKey(entity);
            //更新日报内容表
            newsContent.setId(entity.getId());
            newsContentMapper.updateByPrimaryKey(newsContent);
        }else {
            Date date = new Date();
            entity.setCommendTime(date);
            //保存日报文章
            newsMapper.insert(entity);
            //更新日报内容表
            newsContent.setId(entity.getId());
            newsContentMapper.insert(newsContent);
        }
    }


}
